package org.example.com.editor.util;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * 目录树工具类：生成目录的树形结构字符串
 */
public class DirTreeUtil {
    // 树形结构符号定义
    private static final String BRANCH_NORMAL = "├── "; // 非最后子项的分支
    private static final String BRANCH_LAST = "└── ";  // 最后子项的分支
    private static final String PADDING_EMPTY = "    "; // 非最后子项的层级占位
    private static final String PADDING_BAR = "│   ";   // 最后子项的层级占位

    public static List<String> generateDirTree(File targetDir) throws IOException {
        List<String> treeLines = new ArrayList<>();
        treeLines.add(targetDir.getAbsolutePath());
        List<Boolean> parentIsLastList = new ArrayList<>();
        traverseDir(targetDir, 1, false, parentIsLastList, treeLines);
        return treeLines;
    }

    private static void traverseDir(File currentDir, int depth, boolean isLast,
                                    List<Boolean> parentIsLastList, List<String> treeLines) throws IOException {
        File[] children = currentDir.listFiles();
        if (children == null) {
            String prefix = getPrefix(depth, isLast, parentIsLastList);
            treeLines.add(prefix + "[权限不足，无法访问]");
            return;
        }

        List<File> sortedChildren = new ArrayList<>(List.of(children));
        sortedChildren.sort(Comparator.comparing(File::isFile).thenComparing(File::getName));

        for (int i = 0; i < sortedChildren.size(); i++) {
            File child = sortedChildren.get(i);
            boolean isChildLast = (i == sortedChildren.size() - 1); // 当前子项是否是最后一个

            parentIsLastList.add(isLast);
            String prefix = getPrefix(depth, isChildLast, parentIsLastList);
            parentIsLastList.remove(parentIsLastList.size() - 1); // 回溯，不影响下一个子项

            String childName = child.isDirectory() ? child.getName() + "/" : child.getName();
            treeLines.add(prefix + childName);

            if (child.isDirectory()) {
                parentIsLastList.add(isLast);
                traverseDir(child, depth + 1, isChildLast, parentIsLastList, treeLines);
                parentIsLastList.remove(parentIsLastList.size() - 1); // 回溯
            }
        }
    }

    private static String getPrefix(int depth, boolean isLast, List<Boolean> parentIsLastList) {
        StringBuilder prefix = new StringBuilder();

        for (int i = 0; i < parentIsLastList.size(); i++) {
            prefix.append(parentIsLastList.get(i) ? PADDING_EMPTY : PADDING_BAR);
        }

        prefix.append(isLast ? BRANCH_LAST : BRANCH_NORMAL);

        return prefix.toString();
    }


    public static String validateDir(String path) {
        File target;
        if (path == null || path.trim().isEmpty()) {
            // 无参数：用当前工作目录
            target = new File(System.getProperty("user.dir"));
        } else {
            target = new File(path);
        }

        if (!target.exists()) {
            return "路径不存在：" + target.getAbsolutePath();
        }
        if (!target.isDirectory()) {
            return "不是目录：" + target.getAbsolutePath();
        }
        return null; // 合法
    }
}